<template>
  <page-header-wrapper>
    <page-tab-list>
    </page-tab-list>
    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="clearData">
          <a-radio-button :value="0"> 全部 </a-radio-button>
          <a-radio-button :value="1"> 已更新 </a-radio-button>
          <a-radio-button :value="2"> 未更新 </a-radio-button>
        </a-radio-group>
      </div>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <span slot="goods" slot-scope="scope">
          <!-- {{ scope.info.info_name }} -->
          <div class="goods-info">
            <img class="goods-pic" :src="scope.cover_pic" alt="" />
            <div class="info-box">
              <a-tooltip placement="top">
                <template slot="title">
                  <span class="goods-name">{{ scope.name }} </span>
                </template>
                <div style="font-weight: bold; line-height: 25px; width: 160px" class="of_el">
                  {{ scope.name }}
                </div>
              </a-tooltip>
            </div>
          </div>
        </span>
        <span slot="supplier" slot-scope="scope">
          <!-- {{ scope.info.info_name }} -->
          <div class="goods-info">
            <img class="goods-pic" :src="scope.logo" alt="" />
            <div class="info-box">
              <a-tooltip placement="top">
                <template slot="title">
                  <span class="goods-name">{{ scope.supplier_name }} </span>
                </template>
                <div style="font-weight: bold; line-height: 25px; width: 160px" class="of_el">
                  {{ scope.supplier_name }}
                </div>
                <div style="font-weight: bold; line-height: 25px; width: 160px" class="of_el">
                  {{ scope.phone }}
                </div>
              </a-tooltip>
            </div>
          </div>
        </span>
        <span slot="is_update" slot-scope="text">
          <!-- <a-tag color="#87d068" v-if="text == 1">
            增加
          </a-tag>
          <a-tag color="#f50" v-else>
            减少
          </a-tag> -->

          <span class="tag" :style="text | orderStatus"></span>
          <span>
            {{ getStatus(text) }}
          </span>
        </span>
        <span slot="is_audit" slot-scope="text">
          <!-- <a-tag color="#87d068" v-if="text == 1">
            增加
          </a-tag>
          <a-tag color="#f50" v-else>
            减少
          </a-tag> -->

          <span class="tag" :style="text | orderStatus"></span>
          <span>
            {{ getStatus2(text) }}
          </span>
        </span>
        <span slot="updated_at" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <span slot="created_at" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <span class="table-action" slot="action" slot-scope="text, goods">
          <a :disabled="(!goods.is_audit || goods.is_update) ? true : false" @click="toBindSku(goods.cloud_goods_id)">更新</a>
        </span>
      </a-table>
    </a-card>
    <!-- 绑定商品的弹窗 -->
    <a-modal title="商品列表" width="1000px" okText="确认导入" v-model="showBind" @ok="leadGood" :destroyOnClose="true">
      <a-table
        :columns="skuColumns"
        :data-source="currentCloud.attrs"
        bordered
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="false"
      >
        <div slot="attr_list" slot-scope="text, goods">
          {{ text.map((el) => el.attr_name).join('-') }}
        </div>
        <div slot="pic" slot-scope="text">
          <img :src="text" alt="" style="width: 48px; height: 48px; border-radius: 2px" />
        </div>
        <div slot="no" slot-scope="text">
          <a-tooltip>
            <template slot="title">
              {{ text }}
            </template>
            <span>{{ text }}</span>
          </a-tooltip>
        </div>
        <span slot="price" slot-scope="text"> ￥{{ text || 0 }} </span>
      </a-table>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { getCloudGoodsUpdateLog, supplierGoods } from '@/api/purchase/purchase'
import tabList from '@/views/saas/components/tabList.vue'
import { cyyUploadImg } from '@/components/CyyUi'
import { leadGood } from '@/api/purchase/business'
const columns = [
  {
    title: '商品信息',
    scopedSlots: {
      customRender: 'goods',
    },
    width: 220,
  },
  {
    title: '供货商信息',
    scopedSlots: {
      customRender: 'supplier',
    },
    width: 220,
  },
  {
    title: '平台是否审核',
    dataIndex: 'is_audit',
    scopedSlots: {
      customRender: 'is_audit',
    },
    width: 180,
  },
  {
    title: '状态',
    dataIndex: 'is_update',
    scopedSlots: {
      customRender: 'is_update',
    },
    width: 180,
  },
  {
    title: '添加时间',
    dataIndex: 'created_at',
    sorter: (a, b) => a.created_at - b.created_at,
    scopedSlots: {
      customRender: 'created_at',
    },
    width: 180,
  },
  {
    title: '修改时间',
    dataIndex: 'updated_at',
    sorter: (a, b) => a.updated_at - b.updated_at,
    scopedSlots: {
      customRender: 'updated_at',
    },
    width: 180,
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
    width: 180,
  },
]

const skuColumns = [
  {
    title: '规格名称',
    dataIndex: 'attr_list',
    scopedSlots: {
      customRender: 'attr_list',
    },
  },
  {
    title: '成本价',
    dataIndex: 'price',
    scopedSlots: {
      customRender: 'price',
    },
    sorter: (a, b) => a.price - b.price,
  },
  {
    title: '售价',
    dataIndex: 'sale_price',
    scopedSlots: {
      customRender: 'sale_price',
    },
    sorter: (a, b) => a.sale_price - b.sale_price,
  },
  {
    title: '库存',
    dataIndex: 'num',
    sorter: (a, b) => a.num - b.num,
  },
  // {
  //   title: '重量(g)',
  //   dataIndex: 'weight',
  //   sorter: (a, b) => a.weight - b.weight
  // },
  {
    title: '规格图片',
    dataIndex: 'pic',
    scopedSlots: {
      customRender: 'pic',
    },
  },
  {
    title: '规格货号',
    dataIndex: 'no',
    width: 100,
    scopedSlots: {
      customRender: 'no',
    },
  },
]

export default {
  components: {
    tabList,
    cyyUploadImg,
  },

  filters: {
    orderStatus(val) {
      if (val == 0) {
        return 'background-color:#faad14;'
      }

      if (val == 1) {
        return 'background-color:#65c817;'
      }
    },
  },

  computed: {
    getStatus() {
      return (status) => {
        let str

        switch (status) {
          case 0:
            str = '未更新'
            break

          case 1:
            str = '已更新'
            break
        }

        return str
      }
    },
    getStatus2() {
      return (status) => {
        let str

        switch (status) {
          case 0:
            str = '未审核'
            break

          case 1:
            str = '已审核'
            break
        }

        return str
      }
    },
  },

  data() {
    return {
      tabList: [
        {
          key: 'purchaseGoodsList',
          name: '云仓选品库',
        },
        {
          key: 'goodsCloudList',
          name: '我的云仓',
        },
        {
          key: 'purchaseTransRecord',
          name: '云仓转单',
        },
        {
          key: 'businessPurchaseBalance',
          name: '我的账户',
        },
        {
          key: 'purchaseSetting',
          name: '云仓设置',
        },
        {
          key: 'purchaseGoodsUpdateLog',
          name: '商品更新记录',
        },
        // {
        //     key:'businessPurchaseRecharge',
        //     name:'充值记录'
        // }
      ],
      // 搜索参数
      queryParam: {
        page: 1,
        limit: 10,
        status: 0,
      },
      selectedRowKeys: [],
      columns,
      list: [],
      totalCount: 0,
      mch: {
        balance: '0.00',
      },
      showBind: false,
      // 当前正在操作的云仓商品
      currentCloud: {},
      // 当前操作的云仓规格
      currentCloudSku: {},
      skuColumns,
    }
  },

  created() {
    this.getList()
  },

  methods: {
    async toBindSku(id) {
      let res = await supplierGoods({ id: id })
      if (res.code === 0) {
        let goods_info = res.data.list[0]
        goods_info.attrs = JSON.parse(goods_info.attrs)
        this.currentCloud = goods_info
        this.showBind = true
      } else {
        return null
      }
    },
    clearData() {
      this.queryParam.page = 1
      this.getList()
    },

    // 分页请求
    tablePageChange(e) {
      this.queryParam.page = e
      this.getList()
    },
    // 获取数据
    async getList() {
      const hide = this.$message.loading('加载中...', 1000)
      const res = await getCloudGoodsUpdateLog(this.queryParam)
      if (res.code == 0) {
        this.list = res.data.data
        this.totalCount = res.data.totalCount
      }
      hide()
    },
    // 绑定和解绑操作
    async leadGood() {
      let goods = JSON.parse(JSON.stringify(this.currentCloud))
      // if (!Array.isArray(goods.pic_list)) {
      //   goods.pic_list = JSON.parse(goods.pic_list)
      // }
      // if (!goods.goods_pic_list) {
      //   goods.goods_pic_list = JSON.parse(JSON.stringify(goods.pic_list))
      //   goods.goods_pic_list.unshift({ pic_url: goods.pic_url })
      // }
      // goods.content = goods.desc
      const res = await leadGood({
        id: goods.id,
      })
      if (res.code == 0) {
        this.$message.success('导入成功~')
        this.showBind = false
        await this.getList()
      } else {
        this.$message.error(res.msg)
      }
    },
  },
}
</script>
<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 15px;
  }
}
.tip {
  line-height: normal;
  margin-bottom: 0;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
}

::v-deep {
  .card_top_padding .ant-card-body {
    padding-bottom: 0;
  }
}

.goods-info {
  display: flex;
  align-items: flex-start;
  position: relative;
  .goods-pic {
    width: 48px;
    height: 48px;
    border-radius: 4px;
    display: block;
  }
  .info-box {
    margin-left: 24px;
    margin-right: 5px;
    .goods-name {
      width: 185px;
      font-size: 14px;
      line-height: 22px;
      color: rgba(0, 0, 0, 0.65);
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }
  }
}

.tag {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-right: 4px;
}
.money {
  display: flex;
  flex-direction: column;
  padding-top: 16px;
  div {
    font-size: 26px;
    font-weight: 600;
  }
}
</style>
